/*
 * Copyright (C) 2019 ~ 2021 UnionTech Software Technology Co., Ltd.
 *
 * Author:     caitao <caitao@uniontech.com>
 *
 * Maintainer: caitao <caitao@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "csvwidget.h"

#include <QDebug>
#include <QVBoxLayout>

CSVWidget::CSVWidget(QWidget *parent)
    : QWidget(parent)
    , m_exportBtn(nullptr)
    , m_importBtn(nullptr)
{
    initUi();
    initConnection();
}

CSVWidget::~CSVWidget()
{
}

#include <DMessageBox>

#include <unistd.h> // fork(3), chdir(3), sysconf(3), fsync()

void CSVWidget::exportWhitelistTemplate()
{
    QString savePath = DFileDialog::getSaveFileName(this, "选择保存位置", QDir::homePath() + "/1.csv", "*.csv");

    qInfo() << savePath;

    if (savePath.isEmpty()) {
        qInfo() << Q_FUNC_INFO << "export canceled!";
        return;
    }

    QFile file(savePath);

    if (!file.open(QIODevice::OpenModeFlag::WriteOnly)) {
        qInfo() << Q_FUNC_INFO << savePath << "opened failed!";
        return;
    }

    QTextStream ts(&file);

    //    ts << QString("白名单导入模板\n");
    //    ts << QString("（说明：")
    //       << QString("1、策略列需填入数字，0代表可读可写模式，1代表只读模式；")
    //       << QString("2、本文件前3行内容为注释内容，请勿修改，请从第4行开始填写白名单信息。）\n");

    ts << QString("设备名称,")
       << QString("SN,")
       << QString("PID,")
       << QString("VID,")
       << QString("策略（请填入数字，0代表可读可写模式，1代表只读模式）\n");

    fsync(file.handle());
    file.close();
}

void CSVWidget::importWhitelist()
{
    QString filePath = DFileDialog::getOpenFileName(this, "选择文件", QDir::homePath(), "*.csv");

    qInfo() << filePath;

    if (filePath.isEmpty()) {
        qInfo() << Q_FUNC_INFO << "import canceled!";
        return;
    }

    QFile file(filePath);

    if (!file.open(QIODevice::OpenModeFlag::ReadOnly)) {
        qInfo() << Q_FUNC_INFO << filePath << "opened failed!";
        return;
    }

    QTextStream ts(&file);

    ts.readLine(); // 不读取第1行的表头内容
    while (!ts.atEnd()) {
        const QString &lineStr = ts.readLine();
        const QStringList &infoStrList = lineStr.split(",");
        if (5 != infoStrList.size()) {
            qInfo() << Q_FUNC_INFO << infoStrList << "infomations invalid!";
            continue;
        }

        qInfo() << Q_FUNC_INFO << infoStrList;
    }

    fsync(file.handle());
    file.close();
}

void CSVWidget::initUi()
{
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);

    m_exportBtn = new QPushButton(this);
    m_exportBtn->setText("选择导出目录");
    mainLayout->addWidget(m_exportBtn);

    m_importBtn = new QPushButton(this);
    m_importBtn->setText("打开文件");
    mainLayout->addWidget(m_importBtn);
}

void CSVWidget::initConnection()
{
    // connection
    connect(m_exportBtn, &QPushButton::clicked, this, &CSVWidget::exportWhitelistTemplate);
    connect(m_importBtn, &QPushButton::clicked, this, &CSVWidget::importWhitelist);
}
